package cn.tedu._06vehicle.controller;

import cn.tedu._06vehicle.base.response.JsonResult;
import cn.tedu._06vehicle.base.response.StatusCode;
import cn.tedu._06vehicle.mapper.VehicleMapper;
import cn.tedu._06vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._06vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._06vehicle.pojo.entity.Vehicle;
import cn.tedu._06vehicle.pojo.vo.UserLoginVO;
import cn.tedu._06vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Validated
@RestController
@RequestMapping("/v1/vehicle/")
@Tag(name="02-车辆管理")
@Slf4j
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order=10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam param, HttpSession session){
        log.debug("车辆信息："+param);
        //校验登录状态
        UserLoginVO loginVo=(UserLoginVO) session.getAttribute("user");
//        if (loginVo==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Vehicle vehicle=new Vehicle();
        BeanUtils.copyProperties(param,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUser_id(loginVo.getId());
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.OK();
    }

    @GetMapping("list")
    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order=20)
    @Parameters(value = {
            @Parameter(name="brand"),
            @Parameter(name="license"),
            @Parameter(name="query",hidden = true)
    })
    public JsonResult listVehicle(VehicleListQuery query,HttpSession session){
        log.debug("查询参数："+query);
        UserLoginVO loginVo=(UserLoginVO) session.getAttribute("user");
//        if (loginVo==null){//校验是否登录状态
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        //获取用户id
        Long user_id=loginVo.getId();
        List<VehicleListVO> listVo=vehicleMapper.listVehicle(query,user_id);
        log.debug("查询结果："+listVo);
        return JsonResult.OK(listVo);

    }

    @PostMapping("delete/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order=30)
    @Parameter(name="id")
    public JsonResult deleteVehicle(@PathVariable @Range(min =1L,max=500L,message="车辆id范围1-500") Long id, HttpSession session) throws IllegalArgumentException {
        //手动抛出异常，用户演示全局异常处理器的使用
        if (id<0){
            throw new IllegalArgumentException("车辆id不能小于0");
        }
        log.debug("车辆id："+id);
        UserLoginVO loginVo=(UserLoginVO) session.getAttribute("user");
//        if (loginVo==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Long user_id=loginVo.getId();

        int rows=vehicleMapper.deleteVehicleById(id,user_id);
        if (rows>0){
            return JsonResult.OK();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }



}
